package MTG::MtgRu;
use Moose;

use Data::Dumper;
use strict;
use utf8;
use XML::Simple;


use base "MTG";

has 'cur_set' => ('is' => 'rw', default => sub { { } });

my $config = 
{
	'Magic 2011' =>
	{
		'set' => 'magic 2011',
		'url' => 'http://www.mtg.ru/cards/search.phtml?Grp=M11&page=',
		'code'=> 'm11', 
	},
#	'Magic 2010' =>
#	{
#		'set'=> 'magic 2010',
#		'url' => 'http://www.mtg.ru/cards/search.phtml?Grp=M10&page=',
#		'code' => 'm10',
#	},
#	'Scars of Mirrodin' =>
#	{
#		'set' => 'scars of mirrodin',
#		'url' => 'http://www.mtg.ru/cards/search.phtml?Grp=SOM&page=',
#		'code' => 'som'
#	}
	
};
our @cards;
sub run
{
	my ($self) = @_;
	
	foreach my $set (keys %$config)
	{	
		printf("Work With %s\n", $set);		
		$self->cur_set($config->{$set});
		$self->parse();
	}
	
	$self->write_xml();
	
}

sub write_xml
{
	my ($self) = @_;
	
	open my $fh, '>:encoding(utf-8)', "db.xml"	;
	XMLout({'card' => \@cards}, OutputFile => $fh);
	close $fh;
}

sub parse
{
	my $self = shift;
	
	foreach my $page_num (1..50)
	{ 
		my $page = $self->cur_set->{'url'} . $page_num;

		$self->www->get($page);
		
		
		$self->clean_tree();
		$self->tree->parse($self->www->content());
		$self->tree->normalize_content();
		my @divs =   $self->tree->find_by_tag_name('div');
		my $count_cards = 0;
		
		foreach my $div (@divs)
		{
			
			my $card;
			$card->{'code'} = $self->cur_set->{'code'};
			if (!$div->attr('class') || $div->attr('class') !~ /color/io)
			{
				next;
			}
			
			my @tables = $div->find_by_tag_name('table');
					
			my @tds = $tables[0]->find_by_tag_name('td');
			($card->{'eng_name'},$card->{'rus_name'}) = ($1, $2) if ($tds[1]->as_text =~ /^\s*(.+?)\s*\/\s*\/\s*(.+?)\s*$/);
			($card->{'id'}, $card->{'total'}) = ($1, $2) if ($tds[3]->as_text() =~ /\s*(\d+)\s*\/\s*(\d+)\s*/);
			
			undef @tds;
			@tds = $tables[1]->find_by_tag_name('td');
			
			foreach my $c ($tds[0]->find_by_tag_name('img'))
			{
				$card->{'cost'} .= $c->attr('alt'); 
			}
			
			$card->{'set'} = ($tds[1]->find_by_tag_name('nobr'))[0]->as_text();
			 
			($card->{'attack'}, $card->{'block'}) = ($1, $2)  if (($tds[2]->content_list())[1] =~ /\s*(.+?)\s*\/\s*(.+?)\s*/);
			
			if ($card->{'attack'})
			{
				my $len = length($card->{'attack'}) - 3;
				$card->{'attack'} = substr $card->{'attack'}, -$len;	
			}
			
			
			foreach my $c ($tds[3]->find_by_tag_name('img'))
			{
				$card->{'rare'} .= $c->attr('alt'); 
			}
			
			$card->{'type'} = $tds[4]->find_by_attribute('id', 'noOracle_Type')->as_text;
			$card->{'type'} = $1 if ($card->{'type'} =~ /^\s*(.+?)\s*\//i);
			
			undef @tds;
			@tds = $tables[2]->find_by_tag_name('td');
			
			if ($tds[0]->find_by_attribute('width','*'))
			{
				my @a = $tds[0]->find_by_attribute('width','*')->content_list();
				$card->{'rus_quote'} = $a[3]->as_text;
				$card->{'eng_quote'} = $a[0]->as_text; 
			}
			
			
			foreach my $d ($div->find_by_tag_name('div'))
			{
				if ($d->id && $d->id eq 'NoCardInfo')
				{
					
					my @childs = $d->content_list();
					my $righthtml = 0;
					foreach my $elm(@childs)
					{
						if (ref($elm) ne '' && $elm->tag eq 'span')
						{
							$righthtml = 1;
						}
					}
					
					my $hr = $d->find_by_attribute('id', 'HR1');
					
					if ($hr && $hr->right)
					{
						my $span = $hr->right;
						
						if ($span)
						{
							$card->{'rus_text'} = $span->as_text;
#							$span->delete();
						}
						
					}

					if ($righthtml)
					{
						$card->{'eng_text'} = $d->as_text();
					}
					else
					{
						my $t = $d->as_HTML;
						$t =~ s#id="HR1"(.*)$##i;
						$t =~ s#<br />#\n#gio;
						$t =~ s#<.+?>##gio;
						$t =~ s#<hr##io;
						
						$card->{'eng_text'} = $t;
					}
				}
			}
			push @cards, $card;
			warn Dumper($card);die;
			$count_cards++;
		}
		
		if (!$count_cards)
		{
			last;
		}
	}
}


1;